﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

 
namespace mojoPortal.Web.RS.SBM.Telerik.View
{
    public partial class RSViewInventory : RSBasePage
    {
        private int _companyid = 0;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeReadyServicePage("Inventory");

            RSUserIdentity userIdentity = GetRSUserIdentity();
            _companyid = userIdentity.RSCompanyId;

            if (!Page.IsPostBack)
            {
                //88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
                //RadTreeList1.ExpandedIndexes.Add(new TreeListHierarchyIndex { LevelIndex = 0, NestedLevel = 0 });
                //RadTreeList1.ExpandedIndexes.Add(new TreeListHierarchyIndex { LevelIndex = 1, NestedLevel = 0 });
                //RadTreeList1.ExpandedIndexes.Add(new TreeListHierarchyIndex { LevelIndex = 2, NestedLevel = 0 });
                //RadTreeList1.ExpandedIndexes.Add(new TreeListHierarchyIndex { LevelIndex = 3, NestedLevel = 0 });
                BuildExpandedIndexes(_companyid);

            }

        }
        
        protected void btnAddInventory_Click(Object sender, EventArgs e)
        {
            Response.Redirect("~/RS/SBM/RSManageProduct.aspx?action=add");
        }

        private void BuildExpandedIndexes(int companyid)
        {
            String strSQL = "select * from rs_tblinventory where (ParentId is null or ParentId=0) and companyid=@companyid";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);
            SqlDataReader myDataReader;

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (myDataReader.HasRows)
            {
                int i = 0;
                while (myDataReader.Read())
                {
                    //fulladdress = myDataReader["LocationName"].ToString() + " - ";
                    RadTreeList1.ExpandedIndexes.Add(new TreeListHierarchyIndex { LevelIndex = i, NestedLevel = 0 });
                    i = i + 1;
                }
            }
            myConnection.Close();
        }

        protected void RadTreeList1_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            RadTreeList1.DataSource = InventoryData.GetData(_companyid);
        }


        protected void RadTreeList1_ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
        {
            if (e.Item is TreeListDataItem)
            {
                TreeListDataItem dataItem = e.Item as TreeListDataItem;
                HyperLink editLink = (HyperLink)e.Item.FindControl("hlProductLink");
                editLink.Attributes["href"] = "../../RSManageProduct.aspx?action=inventory&productid=" + dataItem["ProductID"].Text.Trim();
            }
        }
    }
    public class InventoryData
    {
        public static List<Part> GetData(int companyid)
        {
            List<Part> list = new List<Part>();

            string productid;
            string productname;
            string quantity;
            decimal? price;
            bool instock;
            string parentid;

            if (companyid > 0)
            {
                String strSQL = "SELECT Productid, ProductName, UnitsInStock, UnitPrice, ParentID FROM rs_tblInventory WHERE CompanyId=@CompanyId AND Discontinued=0 ORDER BY ParentId, ProductId";
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[RSSBMCommonFunctions.GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(strSQL, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@Companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    while (myDataReader.Read())
                    {
                        productid = myDataReader["productid"].ToString();
                        productname = myDataReader["productname"].ToString();
                        price = Convert.ToDecimal(myDataReader["unitprice"].ToString());
                        quantity = myDataReader["unitsinstock"].ToString();
                        parentid = myDataReader["parentid"].ToString();
                        instock = false;
                        if (quantity.Length > 0)
                        {
                            if (Convert.ToInt16(quantity) > 0)
                                instock = true;
                        }

                        list.Add(new Part(productid, productname, quantity, price, instock, parentid));
                    }

                }

                myConnection.Close();

            }
            return list;
        }
    }

    public class Part
    {
        public string ProductID { get; set; }
        public string ProductName { get; set; }
        public string Quantity { get; set; }
        public decimal? Price { get; set; }
        public bool InStock { get; set; }
        public string ParentID { get; set; }

        public Part(string productid, string productname, string quantity, decimal? price, bool instock, string parentid)
        {
            ProductID = productid;
            ProductName = productname;
            Quantity = quantity;
            Price = price;
            InStock = instock;
            ParentID = parentid;
        }
    }

}